package org.jeecg.modules.kuner.sso;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.constant.CommonConstant;
import org.jeecg.config.shiro.IgnoreAuth;
import org.jeecg.modules.kuner.sso.vo.ModifyUserInfoVO;
import org.jeecg.modules.kuner.sso.vo.UsernameAndPasswordLgVO;
import org.jeecg.modules.putil.wx.WxUtil;
import org.jeecg.modules.putil.wx.vo.WxAutoLoginVO;
import org.jeecg.modules.putil.wx.vo.WxUserEncryptionVO;
import org.jeecg.modules.system.entity.SysUser;
import org.jeecg.modules.system.service.ISysUserService;

import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @Description: 单点登录
 * @Author: Planifolia.Van
 * @Date: 2024/12/24 10:31
 */
@Api(tags = "小程序-单点登录接口")
@RestController
@RequestMapping("/api/sso")
public class SsoApiController {

    @Resource
    private WxUtil wxUtil;

    @Resource
    private ISysUserService sysUserService;

    /**
     * 根据code获取OpenId或者自动登录
     *
     * @param code 获取OpenId需要的Code信息
     * @return 获取到的OpenId或者登录信息
     */
    @IgnoreAuth
    @ApiOperation(value = "获取OpenId或者自动登录", notes = "调用该接口获取OpenId，若用户之前注册过了该接口会直接返回登录信息，若未注册则只会返回openId，sessionKey")
    @GetMapping("/wxGetOpenIdOrLogin")
    public Result<WxAutoLoginVO> wxGetOpenIdOrLogin(String code) {
        WxAutoLoginVO userOpenId = wxUtil.getUserOpenId(code);
        SysUser sysUser = sysUserService.getUserByOpenId(userOpenId.getOpenid());
        if (sysUser == null) {
            return Result.OK("获取成功，用户尚未注册！", userOpenId);
        }
        String token = sysUserService.autoLogin(sysUser);
        sysUser.doDesensitization();
        userOpenId.setToken(token);
        userOpenId.setLoginUser(sysUser);
        return Result.OK("获取成功，用户已经注册，自动登录！", userOpenId);
    }

    /**
     * 微信自动登录与注册
     *
     * @param wxUserEncryptionVO 微信加密的用户数据与OpenId
     * @return 登陆后的用户信息与token
     */
    @IgnoreAuth
    @ApiOperation(value = "微信自动注册与登录", notes = "当调用接口 ‘获取OpenId或者自动登录’ 返回用户尚未注册的时候需要调用该接口进行注册操作！")
    @PostMapping("/wxAutoRegistAndLogin")
    public Result<WxAutoLoginVO> wxAutoRegistAndLogin(@RequestBody WxUserEncryptionVO wxUserEncryptionVO) {
        SysUser sysUser = wxUtil.wxUserInfoToSysUser(wxUserEncryptionVO);
        sysUserService.addUserWithRole(sysUser, CommonConstant.ROLE_ID_MEMBER);
        String token = sysUserService.autoLogin(sysUser);
        WxAutoLoginVO autoLoginVO = new WxAutoLoginVO();
        autoLoginVO.setToken(token);
        autoLoginVO.setLoginUser(sysUser.doDesensitization());
        return Result.OK("自动注册及登录成功", autoLoginVO);
    }

    /**
     * 工作人员登录操作
     *
     * @param usernameAndPasswordLgVO 登录必要的参数信息
     * @return 登录结果
     */
    @ApiOperation("员工登录")
    @PostMapping("/workerLogin")
    @IgnoreAuth
    public Result<?> workerLogin(@RequestBody UsernameAndPasswordLgVO usernameAndPasswordLgVO) {
        WxAutoLoginVO wxAutoLoginVO = sysUserService.classicalLogin(usernameAndPasswordLgVO);
        return Result.OK("查询成功！", wxAutoLoginVO);
    }


    @ApiOperation("修改用户必要信息")
    @PostMapping("/modifyUserInfo")
    public Result<?> modifyUserInfo(@RequestBody ModifyUserInfoVO modifyUserInfoVO) {
        SysUser sysUser = sysUserService.modifyUserInfo(modifyUserInfoVO);
        return Result.OK("操作成功！", sysUser);
    }


}
